package com.ruoyi.common.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.ruoyi.common.config.LogoConfig;
import com.ruoyi.common.config.RuoYiConfig;

import javax.imageio.ImageIO;
import java.awt.BasicStroke;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

/**
 * @ClassName imageUtils
 * @Description 生成二维码工具
 * @Author yangtao2
 * @Date 2022/4/29 15:00
 * @Version 1.0
 **/
public class QrCodeUtils {

	private static final int BLACK = 0xFF000000;
	private static final int WHITE = 0xFFFFFFFF;

	public static boolean generateCode(String phone) throws WriterException, IOException {
		// 这里是URL，扫描之后就跳转到这个界面
		String text = "https://www.yangtaoer.com.cn/ry/rnx/register";
		// 图片生成的位置
		String path = RuoYiConfig.getUploadPath();
		int width = 400;
		int height = 400;
		// 二维码图片格式
		String format = "jpg";
		// 设置编码，防止中文乱码
		Hashtable<EncodeHintType, Object> ht = new Hashtable<>();
		ht.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		// 设置二维码参数(编码内容，编码类型，图片宽度，图片高度,格式)
		BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, ht);
		// 生成二维码(定义二维码输出服务器路径)
		File outputFile = new File(path);
		if (!outputFile.exists()) {
			// 创建文件夹
			outputFile.mkdirs();
		}
		int b_width = bitMatrix.getWidth();
		int b_height = bitMatrix.getHeight();
		// 建立图像缓冲器
		BufferedImage image = new BufferedImage(b_width, b_height, BufferedImage.TYPE_3BYTE_BGR);
		for (int x = 0; x < b_width; x++) {
			for (int y = 0; y < b_height; y++) {
				image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);
			}
		}
		// 生成二维码
		File qr = new File(path + "/invite_" + phone +"." + format);
		ImageIO.write(image, format, qr);
		//添加logo
		addLogoToQRCode(
				qr.getPath(),
				RuoYiConfig.getLogoPath(),
				new LogoConfig()
		);
		return true;
	}

	/**
	 * 给二维码图片添加Logo
	 *
	 * @param qrCodePath 二维码路径
	 * @param logoPath   logo路径
	 * @param logoConfig logo 配置文件
	 */
	public static void addLogoToQRCode(String qrCodePath, String logoPath, LogoConfig logoConfig) {
		try {
			File qrCodeFile = new File(qrCodePath);
			File logoFile = new File(logoPath);
			if (!qrCodeFile.isFile() || !logoFile.isFile()) {
				System.out.print("file not find origial file !");
			}
			//读取二维码图片，并构建绘图对象
			BufferedImage image = ImageIO.read(qrCodeFile);
			Graphics2D g = image.createGraphics();
			//读取logo文件
			BufferedImage logo = ImageIO.read(logoFile);
			//设置logo的位置
			int widthLogo = image.getWidth() / logoConfig.getLogoPart();
			// 保持二维码是正方形的
			int heightLogo = image.getWidth() / logoConfig.getLogoPart();
			// 计算图片放置位置（居中）
			int x = (image.getWidth() - widthLogo) / 2;
			int y = (image.getHeight() - heightLogo) / 2;
			// 开始绘制图片
			g.drawImage(logo, x, y, widthLogo, heightLogo, null);
			g.drawRoundRect(x, y, widthLogo, heightLogo, 10, 10);
			g.setStroke(new BasicStroke(logoConfig.getBorder()));
			g.setColor(logoConfig.getBorderColor());
			g.drawRect(x, y, widthLogo, heightLogo);
			g.dispose();
			ImageIO.write(image, "jpg", new File(qrCodePath));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
